package cn.qyd.kaoshi;

import java.util.HashMap;
import java.util.LinkedList;

/**
 * @author 邱运铎
 * leecode 994. 腐烂的橘子
 * @date 2024-03-23 22:14
 */
public class RottenOrange {
    public static void main(String[] args) {
        RottenOrange or = new RottenOrange();
//        int[][] grid = new int[][]{{2,1,1},{1,1,0},{0,1,1}};
        int[][] grid = new int[][]{{2,1,1},{0,1,1},{1,0,1}};
        int result = or.orangesRotting(grid);
        System.out.println(result);
    }
    public int orangesRotting(int[][] grid) {
        //最开始新鲜的橘子数目
        int fresh_start = 0;
        //经过一轮时间后新鲜橘子的数目
        int fresh_end = 0;
        //最开始腐烂的橘子数目
        int rot_start = 0;
        //经过一轮时间后腐烂的橘子数目
        int rot_end = 0;
        //需要的时间轮次
        int time = -1;
        int m = grid.length;
        int n = grid[0].length;
        HashMap<String,String> freshMap = new HashMap<>();
        HashMap<String,String> rotMap = new HashMap<>();
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == 1) {
                    freshMap.put(i+"_"+j,"1");
                }
                if (grid[i][j] == 2) {
                    rotMap.put(i+"_"+j,"2");
                }
            }
        }
        System.out.println("---初始状态-----");
        System.out.println(freshMap);
        System.out.println(rotMap);
        do {
            time++;
            fresh_start = freshMap.size();
            rot_start = rotMap.size();
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    if (grid[i][j] == 2 && rotMap.containsKey(i + "_" + j)) {
                        if (i - 1 >= 0 && grid[i - 1][j] == 1) {
                            grid[i - 1][j] = 2;
                            freshMap.remove((i - 1) + "_" + j);
                        }
                        if (i + 1 < m && grid[i + 1][j] == 1) {
                            grid[i + 1][j] = 2;
                            freshMap.remove((i + 1) + "_" + j);
                        }
                        if (j - 1 >= 0 && grid[i][j - 1] == 1) {
                            grid[i][j - 1] = 2;
                            freshMap.remove(i + "_" + (j - 1));
                        }
                        if (j + 1 < n && grid[i][j + 1] == 1) {
                            grid[i][j + 1] = 2;
                            freshMap.remove(i + "_" + (j + 1));
                        }
                    }
                }
            }
            System.out.println("第--" + time + "轮次后新鲜橘子和腐烂橘子的变化--");
            System.out.println(freshMap);
            System.out.println(rotMap);
            fresh_end = freshMap.size();
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    if (grid[i][j] == 2) {
                        rotMap.put(i + "_" + j, "");
                    }
                }
            }
            rot_end = rotMap.size();
            System.out.println("-----------------------");
            System.out.println(freshMap);
            System.out.println(rotMap);
        } while (fresh_start != fresh_end || rot_start != rot_end);

        if (!freshMap.isEmpty()) {
            return -1;
        }

        return time;
    }
}
